const designFolderDao = require('../dao/designFolderApi');
const util = require('../utilitys');
/**
 * 获取文件夹列表
 * @param ctx
 * @returns {Promise<boolean>}
 * @constructor
 */
exports.getCurrentFolder = async (ctx) => {
    try {
        let result = await designFolderDao.getCurrentFolder(ctx);
        if (result.length > 0) {
            ctx.body = util.resultSuccessJson(undefined, undefined, result);
        } else {
            ctx.body = util.resultSuccessJson(undefined, '请求错误', {});
        }
    } catch (err) {
        ctx.body = util.resultErrorJson(undefined, err, {});
    }
};

/**
 * 新增一个文件夹
 * @param ctx
 * @returns {Promise<boolean>}
 * @constructor
 */
exports.addOneFolder = async (ctx) => {
    try {
        let result = await designFolderDao.addOneFolder(ctx);
        if (result.length > 0) {
            ctx.body = util.resultSuccessJson(undefined, undefined, result);
        } else {
            ctx.body = util.resultSuccessJson(undefined, '请求错误', {});
        }
    } catch (err) {
        ctx.body = util.resultErrorJson(undefined, err, {});
    }
};

/**
 * 修改当前文件夹的顺序
 * @param ctx
 * @returns {Promise<boolean>}
 * @constructor
 */
exports.changeOneOrder = async (ctx) => {
    try {
        let result = await designFolderDao.changeOneOrder(ctx);
        if (result.length > 0) {
            ctx.body = util.resultSuccessJson(undefined, undefined, result);
        } else {
            ctx.body = util.resultSuccessJson(undefined, undefined, result);
        }
    } catch (err) {
        ctx.body = util.resultErrorJson(undefined, err, {});
    }
};

/**
 * 删除一个文件夹
 * @param ctx
 * @returns {Promise<boolean>}
 * @constructor
 */
exports.deleteOneFolder = async (ctx) => {
    try {
        let result = await designFolderDao.deleteOneFolder(ctx);
        if (result === undefined) {
            result = await designFolderDao.deleteOneFolderFiles(ctx);
            ctx.body = util.resultSuccessJson(undefined, undefined, result);
        } else {
            ctx.body = util.resultErrorJson(undefined, err, result);
        }
    } catch (err) {
        ctx.body = util.resultErrorJson(undefined, err, {});
    }
};

/**
 * 重命名一个文件夹
 * @param ctx
 * @returns {Promise<boolean>}
 * @constructor
 */
exports.renameOneFolder = async (ctx) => {
    try {
        let result = await designFolderDao.renameOneFolder(ctx);
        if (result.length > 0) {
            result = designFolderDao.renameOneFolderFiles(ctx);
            ctx.body = util.resultSuccessJson(undefined, undefined, result);
        } else {
            ctx.body = util.resultSuccessJson(undefined, undefined, result);
        }
    } catch (err) {
        ctx.body = util.resultErrorJson(undefined, err, {});
    }
};
